
package com.netease.xone.itemview;

import protocol.XoneService;
import protocol.meta.Source;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.image.ImageType;
import com.netease.xone.R;
import com.netease.xone.activity.ActivitySourceDetail;
import com.netease.xone.widget.LoadingImageView;

public class RenderSourceView {

    ViewGroup sourceItemClick;
    TextView sourceName;
    TextView subscribeCount;
    TextView subscribeButton;
    ImageView shadowTop;
    ImageView shadowBottom;
    LoadingImageView sourceAvatar;

    int roundSize;

    public RenderSourceView(View v) {
        sourceItemClick = (ViewGroup)v.findViewById(R.id.source_item);
        sourceName = (TextView)v.findViewById(R.id.source_name);
        subscribeCount = (TextView)v.findViewById(R.id.source_subscribe_count);
        subscribeButton = (TextView)v.findViewById(R.id.source_subscribe_button);
        shadowTop = (ImageView)v.findViewById(R.id.source_top_shadow);
        shadowBottom = (ImageView)v.findViewById(R.id.source_bottom_shadow);
        sourceAvatar = (LoadingImageView)v.findViewById(R.id.source_avatar);

        if (sourceAvatar != null) {
            roundSize = v.getResources().getDimensionPixelSize(R.dimen.common_avatar_rounder);
            int size = v.getResources().getDimensionPixelSize(R.dimen.common_avatar_size);
            sourceAvatar.setServerClipSize(size, size);
        }
    }

    public void renderView(final Context context, final Source source, boolean topShadow,
            boolean bottomShadow) {
        if (sourceItemClick != null) {
            sourceItemClick.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ActivitySourceDetail.startActivity(context, source.sourceId, source.sourceName);
                }
            });
        }
        sourceName.setText(source.sourceName);
        subscribeCount.setText(context.getResources().getString(R.string.people_has_subscribed,
                source.subscribeCount));
        subscribeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 订阅或者取消订阅
                if (source.isSubscribed) {
                    XoneService.getInstance().doUnsubscribe(source.sourceId);
                } else {
                    XoneService.getInstance().doSubscribe(-1, source.sourceId);
                }
            }
        });
        if (source.isSubscribed) {
            // 已订阅的状态
            subscribeButton.setTextColor(context.getResources().getColor(R.color.button_gray));
            subscribeButton.setText(R.string.cancel_subscribe);
            subscribeButton.setBackgroundResource(R.drawable.selector_gray_btn);
        } else {
            // 未订阅状态
            subscribeButton.setTextColor(Color.WHITE);
            subscribeButton.setText(R.string.subscribe);
            subscribeButton.setBackgroundResource(R.drawable.selector_blue_btn);
        }
        if (sourceAvatar != null && source.avatar != null) {
            sourceAvatar.setImageResource(R.drawable.avatar_source_default);
            sourceAvatar.setLoadingImage(source.avatar, ImageType.RoundMemCache, roundSize);
        }

        // 阴影相关//已去掉阴影
        // if (topShadow) {
        // shadowTop.setVisibility(View.VISIBLE);
        // } else {
        // shadowTop.setVisibility(View.GONE);
        // }
        // if (bottomShadow) {
        // shadowBottom.setVisibility(View.VISIBLE);
        // } else {
        // shadowBottom.setVisibility(View.GONE);
        // }

    }
}
